#!/bin/bash

echo "查看centos 版本"
cat /etc/redhat-release
echo "Linux 查看CPU核心数"
cat /proc/cpuinfo | grep "model name" && cat /proc/cpuinfo | grep "physical id"
echo "Linux查看内存大小"
cat /proc/meminfo | head -n 16
##判断是否安装了docker
docker > /dev/null 2>&1
if [[ $? -eq 0 ]];then
    echo "你已经装过了docker"
    exit
fi

## "判断内核版本是否大于3"
main=`uname -r | awk -F . '{print $1}'`
kernelVersion=`uname -r`
if [ "$main" -gt 3 ]
    then
	    echo "kernel version is :$kernelVersion  "
        echo "重新安装下 contain-SElinux"
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
	    echo "初始化docker安装环境-------start-----------"
        echo "设置防火墙为Iptables并设置空规则"
        systemctl stop firewalld && systemctl disable firewalld
        yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
        echo "关闭SELINUX"
        swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
        setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
        echo "设置系统时区为 中国/上海"
        timedatectl set-timezone Asia/Shanghai
        echo "将当前的UTC时间写入硬件时钟"
        timedatectl set-local-rtc 0
        echo "重启依赖于系统时间的服务"
        systemctl restart rsyslog
        systemctl restart crond
        echo "关闭系统不需要的服务"
        systemctl stop postfix && systemctl disable postfix
        echo "初始化安装环境-------end-----------"
        echo "++++++++++++++++++++++++++++++++++"
        echo "docker安装-------start-----------"
        yum install -y yum-utils device-mapper-persistent-data lvm2

        yum-config-manager \
         --add-repo \
         http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
        ##安装docker-ce遇到报错——Error Packagedocker-ce-rootless-extras-20.10.3-3.el7.x86_64 则先执行下面语句
        # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
        yum update -y && yum install -y docker-ce

        echo "创建/etc/docker目录"
        mkdir /etc/docker

        echo "配置daemon【container的log就导出到/var/log/messages】"
        cat > /etc/docker/daemon.json << EOF
{
    "registry-mirrors": ["https://v1gfruyh.mirror.aliyuncs.com"],
    "log-driver":"syslog",
    "log-opts":{
        "tag": "{{.Name}}",
        "syslog-facility": "local6"
    },
    "data-root": "/home/docker",
    "icc": false,
    "live-restore": true,
    "userland-proxy": false,
    "no-new-privileges": true

}
EOF

        echo "创建存放docker的配置文件"
        mkdir -p /etc/systemd/system/docker.service.d
        echo "配置syslog ,systemd-journald依赖于一个machine id"
        dbus-uuidgen --ensure=/etc/machine-id
        echo "重启journald和rsyslog"
        systemctl start systemd-journald
        systemctl start rsyslog
        echo "重启docker服务"
        systemctl daemon-reload && systemctl restart docker && systemctl enable docker
        echo "审计docker相关的文件和目录"
        cat > /etc/audit/rules.d/docker-audit.rules <<'EOF'
-w /usr/bin/docker -k docker
-w /usr/lib/systemd/system/docker.service -k docker
-w /usr/lib/systemd/system/docker.socket -k docker
-w /usr/bin/docker-containerd -k docker
-w /usr/bin/docker-runc -k docker
-w /etc/docker -k docker
-w /etc/docker/daemon.json -k docker
EOF
        echo " 重启审计服务"
        service auditd restart

        echo "docker-compose 安装"
        pip3 install --upgrade pip
        # 没有开公网的服务器，注意切换 pip 源，解决超时问题
        # 运行 wget http://image-offline.oss-cn-hangzhou.aliyuncs.com/fix/fix_pypi.sh 获取文件 bash fix_pypi.sh "mirrors.aliyun.com"
        pip3 install docker-compose
        source /etc/profile
        docker-compose --version
        echo "docker安装-------end-----------"
    else
	    echo "内核版本 小于3，需先升级内核版本,当前 kernel version is :$kernelVersion "
	    echo "CentOS 7.x系统自带的3.10x内核存在一些Bugs，导致运行的Docker、Kubernetes不稳定。"
	    echo "升级内核----start---------"
        rpm -Uvh http://mirror.ventraip.net.au/elrepo/elrepo/el7/x86_64/RPMS/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
        echo "安装完成后检查 /boot/grub2/grub.cfg 中对应内核 menuentry 中是否包含 initrd16 配置，如果没有，再安装一次"
        yum --enablerepo=elrepo-kernel install -y kernel-lt
        echo "设置开机从新内核启动"
        grub2-set-default "CentOS Linux (5.4.163-1.el7.elrepo.x86_64) 7 (Core)"
        echo "升级内核----end---------"
        echo "重启服务器-------------"
        reboot
fi
